package com.code.petapp.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.code.petapp.R;
import com.code.petapp.adapter.FosterImgAdapter;
import com.code.petapp.bean.AdoptBean;
import com.code.petapp.bean.FosterBean;
import com.code.petapp.databinding.ActivityFosterBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.AdoptUtils;
import com.code.petapp.util.FosterUtils;
import com.code.petapp.util.SPCommonUtils;

import java.util.ArrayList;
import java.util.List;

public class FosterActivity extends BaseActivity implements FosterImgAdapter.OnItemClickListener {
    ActivityFosterBinding binding;
    private RecyclerView recyclerView;//声明RecyclerView
    private FosterImgAdapter adapter1;
    private Context context;
    private List<FosterBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFosterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        list = new ArrayList<>();
        setSpinner();
        context = this;
        recyclerView = binding.recView;
        adapter1 = new FosterImgAdapter(context, list);
        adapter1.setOnItemClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter1);
        setAdd();
    }

    private void setSpinner() {
        String[] options = {"全部", "待审核", "寄养中", "已领回"};

// 创建一个 ArrayAdapter 并设置到 Spinner
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, options);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        binding.spinnerStatus.setAdapter(adapter);

// 添加监听器获取选择的选项
        binding.spinnerStatus.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // 处理选中的选项
                String selectedOption = options[position];
                if (selectedOption.equals("全部")) {
                    load();
                } else {
                    load(selectedOption);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // 没有选中任何选项时的逻辑
                load();
            }
        });
    }

    private void load() {
        list.clear();
        FosterUtils.selectAll(SPCommonUtils.getInstance().getToken(), new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(ArrayList<FosterBean> beanList) {
                list.addAll(beanList);
                adapter1.notifyDataSetChanged();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }

    private void load(String status) {
        list.clear();
        FosterUtils.selectStatus(SPCommonUtils.getInstance().getToken(), status, new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(ArrayList<FosterBean> beanList) {
                list.addAll(beanList);
                adapter1.notifyDataSetChanged();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }

    private void setAdd() {
        binding.faAdd.setOnClickListener(v -> {
            startActivity(new Intent(FosterActivity.this, PostFosterActivity.class));
            finish();
        });
    }

    @Override
    public void editOnClick(View parent, int position) {
        String name = list.get(position).getName();
        String time = list.get(position).getTime();
        String days = list.get(position).getDays();
        int id = list.get(position).getId();

        Intent intent = new Intent(FosterActivity.this, FosterEditActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("time", time);
        intent.putExtra("days", days);
        intent.putExtra("id", id);
        startActivity(intent);
        finish();
    }

    @Override
    public void endOnClick(View parent, int position) {
        int id = list.get(position).getId();
        AlertDialog.Builder builder = new AlertDialog.Builder(FosterActivity.this);
        builder.setIcon(R.drawable.icon);
        builder.setTitle("确定结束寄养？");
        builder.setMessage("你的寄养将被取消，房间将被释放。");
        builder.setPositiveButton("结束",
                        (DialogInterface dialog, int arg1) -> {
                            dialog.dismiss();
                            String status="已领回";
                            FosterBean fosterBean=new FosterBean();
                            fosterBean.setId(id);
                            fosterBean.setStatus(status);
                            FosterUtils.update(SPCommonUtils.getInstance().getToken(), fosterBean, new HttpListener<>() {
                                @Override
                                public void success(String data) {
                                    toastShortShow("领回成功");
                                    load();
                                }
                                @Override
                                public void error(int code, String error) {
                                    toastShortShow(error);
                                }
                            });
                        })
                .setNegativeButton("取消",
                        (DialogInterface dialog, int arg1) -> {
                            dialog.dismiss();
                        });
        builder.create().show();
    }

    @Override
    public void cancelOnClick(View parent, int position) {
        int id = list.get(position).getId();
        AlertDialog.Builder builder = new AlertDialog.Builder(FosterActivity.this);
        builder.setIcon(R.drawable.icon);
        builder.setTitle("确定撤销？");
        builder.setMessage("管理员正在火速安排房间。。。");
        builder.setPositiveButton("撤销",
                        (DialogInterface dialog, int arg1) -> {
                            dialog.dismiss();
                            FosterUtils.delete(SPCommonUtils.getInstance().getToken(), id, new HttpListener<>() {
                                @Override
                                public void success(String data) {
                                    toastShortShow("撤销成功");
                                    load();
                                }

                                @Override
                                public void error(int code, String error) {
                                    toastShortShow(error);
                                }
                            });
                        })
                .setNegativeButton("取消",
                        (DialogInterface dialog, int arg1) -> {
                            dialog.dismiss();
                        });
        builder.create().show();

    }
}